package shangguigu_juc.sync;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
class Ticket_1 {
    private int ticketNum;
    private int saleNum;
    public synchronized void sale() {
        if (ticketNum > 0) {
            System.out.println(Thread.currentThread().getName() + ": 卖出第" + (++saleNum) + "张" + " 剩余：" + --ticketNum + "张");
        }
    }
}

public class SaleTicket {
    public static void main(String[] args) {
        Ticket_1 ticket1 = new Ticket_1(30, 0);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    ticket1.sale();
                }
            }
        }, "T1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    ticket1.sale();
                }
            }
        }, "T2").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    ticket1.sale();
                }
            }
        }, "T3").start();
    }
}
